import psycopg2
import pymysql

from fiveinrow import *
from LoginMainWindows import *
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys

information=' '
user_now=' '
Name=''
# 登录界面
class LoginMainWindows(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_LoginMainWindow()
        self.ui.setupUi(self)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.ui.pushButton_Login.clicked.connect(lambda: self.ui.stackedWidget.setCurrentIndex(0))
        self.ui.pushButton_Register.clicked.connect(lambda: self.ui.stackedWidget.setCurrentIndex(1))
        # 通过函数判断输入信息是否正确
        self.ui.pushButton_L_sure.clicked.connect(self.login_in)
        self.ui.pushButton_R_sure.clicked.connect(self.register_in)
        self.show()

    def login_in(self):
        account=self.ui.lineEdit_L_account.text()
        password = self.ui.lineEdit_L_password.text()
        # 创建列表
        account_list = []
        password_list = []
        conn = pymysql.connect(
            host='localhost',
            user='root',
            password='a/15973196174',
            database='mydata',
            charset='utf8mb4',
            cursorclass=pymysql.cursors.DictCursor
            # 游标类型，默认为元组
        )
        cur=conn.cursor()
        cur.execute("select *from users")
        rows=cur.fetchall()
        # print(rows)
        for row in rows:
            account_list.append(row['account'])
            password_list.append(row['password'])
        # print(account_list,password_list)
        conn.commit()
        conn.close()
        for i in range(len(account_list)):
            if len(account) == 0 or len(password) == 0:
                print('输入为空')
                self.ui.stackedWidget_2.setCurrentIndex(1)
            #     非数字用空格
            elif account == account_list[i] and password == password_list[i]:
                print('登录成功')
                # global information
                # information=rows['account']+rows['win']+rows['lose']
                fiveInrow()
                # self.win=MainWindow()
                self.close()
            else:
                print('登录错误')
                self.ui.stackedWidget_2.setCurrentIndex(5)
        # else:
        #     print("Wrong")

    def register_in(self):
        account=self.ui.lineEdit_R_account.text()
        password_1=self.ui.lineEdit_R_password.text()
        password_2=self.ui.lineEdit_R_password_sure.text()
        if len(account)==0 or len(password_1)==0 or len(password_2)==0:
            self.ui.stackedWidget_2.setCurrentIndex(3)
        elif password_1!=password_2:
            self.ui.stackedWidget_2.setCurrentIndex(4)
        else:
            conn = pymysql.connect(
                host='localhost',
                user='root',
                password='a/15973196174',
                database='mydata',
                charset='utf8mb4',
                cursorclass=pymysql.cursors.DictCursor
                # 游标类型，默认为元组
            )
            cur = conn.cursor()
            cur.execute(f"insert into users values('{account}','{password_1}')")
            rows=cur.fetchall()
            conn.commit()
            conn.close()
            self.ui.stackedWidget_2.setCurrentIndex(2)
            global user_now
            user_now = account
            user_win=rows.win
            user_lose=rows.lose
            # 背景音乐
            filepath = r"F:Blank.mp3"  # 打开mp3文件
            pygame.mixer.init()
            pygame.mixer.music.load(filepath)  # 加载MP3文件
            pygame.mixer.music.play()  # 播放mp3文件
            fiveInrow()
            self.close()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = LoginMainWindows()
    sys.exit(app.exec_())